package cn.ivicar.oss.resource;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.oss.controller.StatisticsController;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * 与total_statistic有关的接口
 * Created by Administrator on 2017/5/21.
 */
@Path("stat")
@RequestScoped
public class StatisticResource {
    private static transient final Logger LOGGER = LoggerFactory.getLogger(StatisticResource.class);

    @Inject
    private StatisticsController statisticsController;

    /*
    获得所有设备信息（在线，经纬度）
     */
    @Path("data")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getStatisticData() throws CarsmartException {

        JSONObject totalStat = this.statisticsController.getTotalStat();
        /*JSONObject totalStat = new JSONObject();
        totalStat.put("activateStat", 0);
        totalStat.put("onLineCount", 0);
        totalStat.put("offLineCount", 0);*/

        return CacheUtil.noCache(Response.ok(totalStat)).build();
    }

    /*
    获得离线设备位置
     */
    @Path("map/offline")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getOffLineMapData() throws CarsmartException {

        JSONArray hotMapData = this.statisticsController.getHotMapData(false);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",hotMapData);

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得在线设备位置
     */
    @Path("map/online")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getOnLineMapData() throws CarsmartException {

        JSONArray hotMapData = this.statisticsController.getHotMapData(true);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",hotMapData);

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }

    /*
    获得车辆首次上报位置
     */
    @Path("map/first")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getFirstMapData() throws CarsmartException {

        JSONArray hotMapData = this.statisticsController.getFirstMapData();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",hotMapData);

        return CacheUtil.noCache(Response.ok(jsonObject)).build();
    }


    /*
    获得车辆在线信息（包括在线总数等）
     */
    @Path("activate")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getActivateData() throws CarsmartException {

        JSONArray activateStat = this.statisticsController.getActivateStat();

        return CacheUtil.noCache(Response.ok(activateStat)).build();
    }

}
